/**
 * 文件名：****.java
 * 版权：Copyright Lightkit All Rights Reserved.
 * 描述：*****
 */
package com.lightkits.wms.controller;


import com.lightkits.framework.common.exception.LightKitsBizException;
import com.lightkits.framework.common.model.CheckResult;
import com.lightkits.framework.common.model.IResult;
import com.lightkits.framework.common.utility.object.ObjectUtil;
import com.lightkits.framework.common.utility.web.HttpClient;
import com.lightkits.wms.domain.allot.GetTagMessService;
import com.lightkits.wms.domain.allot.UseAllotParamsService;
import com.lightkits.wms.domain.allot.UseAllotReceiveParamsService;
import com.lightkits.wms.dto.allot.AllotReceiveRequestType;
import com.lightkits.wms.dto.allot.AllotReceiveResponseType;
import com.lightkits.wms.dto.allot.AllotSendRequestType;
import com.lightkits.wms.dto.allot.AllotSendResponseType;
import com.lightkits.wms.dto.allot.GetComMesRequestType;
import com.lightkits.wms.dto.allot.GetComMesResponseType;
import com.lightkits.wms.dto.allot.GetTagMessRequestType;
import com.lightkits.wms.dto.allot.GetTagMessResponseType;
import com.lightkits.wms.dto.allot.model.TagMessageDto;
import io.choerodon.swagger.annotation.Permission;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * AllotController
 *
 * @author jing.wei@hand-china.com
 * Created on 2019/5/21 0:03
 */
@RestController
@RequestMapping("/AllotService")
public class AllotController {
    private final GetTagMessService getTagMessService;
    private final UseAllotParamsService useAllotParamsService;
    private final UseAllotReceiveParamsService useAllotReceiveParamsService;

    @Autowired
    public AllotController(GetTagMessService getTagMessService, UseAllotParamsService useAllotParamsService, UseAllotReceiveParamsService useAllotReceiveParamsService) {
        this.getTagMessService = getTagMessService;
        this.useAllotParamsService = useAllotParamsService;
        this.useAllotReceiveParamsService = useAllotReceiveParamsService;
    }

    @PostMapping(value = "/getTagMess")
    @Permission(permissionPublic = true)
    @ApiOperation(value = "/getTagMess")
    @ResponseBody
    public GetTagMessResponseType getTagMess(@RequestBody(required = false) GetTagMessRequestType request) {
        GetTagMessResponseType response = new GetTagMessResponseType();
        response.setSuccessful(true);
        try {

            CheckResult<TagMessageDto> checkResult = getTagMessService.getTagMes(request.getTagCode(), request.isViaFlag(),
                    request.isBookFlag(),request.getOrganizationId());
            if (!checkResult.isSuccessful() || checkResult.getData() == null) {
                return checkResult.fill(response);
            }
            response.setTagMessageDTO(checkResult.getData());
        } catch (LightKitsBizException e) {
            response.setSuccessful(false);
            String mes = StringUtils.substringAfterLast(e.getMessage(), ":");
            response.setMessage(ObjectUtil.isNullOrEmpty(mes) ? e.getMessage() : mes);

        } catch (Exception e) {
            e.printStackTrace();
            response.setSuccessful(false);
            response.setMessage("get Tag Mes Faild");
        }
        return response;
    }

    @PostMapping(value = "/getComTagMes")
    @Permission(permissionPublic = true)
    @ApiOperation(value = "/getComTagMes")
    @ResponseBody
    public GetComMesResponseType getComTagMes(@RequestBody(required = false) GetComMesRequestType request) {
        GetComMesResponseType response = new GetComMesResponseType();
        response.setSuccessful(true);
        try {
            CheckResult<TagMessageDto> checkResult = getTagMessService.getComTagMes(request.getTagCode(),
                    request.getOrgId(),request.isViaFlag(),request.isBookFlag());
            if (!checkResult.isSuccessful() || checkResult.getData() == null) {
                return checkResult.fill(response);
            }
            response.setTagMessageDTO(checkResult.getData());
        } catch (LightKitsBizException e) {
            response.setSuccessful(false);
            String mes = StringUtils.substringAfterLast(e.getMessage(), ":");
            response.setMessage(ObjectUtil.isNullOrEmpty(mes) ? e.getMessage() : mes);
        } catch (Exception e) {
            e.printStackTrace();
            response.setSuccessful(false);
            response.setMessage("get Com Tag Mes Faild");
        }
        return response;
    }

    @PostMapping(value = "/allotSend")
    @Permission(permissionPublic = true)
    @ApiOperation(value = "/allotSend")
    @ResponseBody
    public AllotSendResponseType allotSend(@RequestBody(required = false) AllotSendRequestType request) {
        AllotSendResponseType response = new AllotSendResponseType();
        response.setSuccessful(true);
        try {
            CheckResult<Long> result = useAllotParamsService.allotSend(request.getHeadDTO(), request.getLineDTOS());
            if (!result.isSuccessful()) {
                return result.fill(response);
            }
            response.setEventId(result.getData());

        } catch (LightKitsBizException e) {
            response.setSuccessful(false);
            String mes = StringUtils.substringAfterLast(e.getMessage(), ":");
            response.setMessage(ObjectUtil.isNullOrEmpty(mes) ? e.getMessage() : mes);

        } catch (Exception e) {
            e.printStackTrace();
            response.setSuccessful(false);
            response.setMessage("allot Send Faild");
        }
        return response;
    }

    @PostMapping(value = "/allotReceive")
    @Permission(permissionPublic = true)
    @ApiOperation(value = "/allotReceive")
    @ResponseBody
    public AllotReceiveResponseType allotReceive(@RequestBody(required = false)
                                                         AllotReceiveRequestType request) {
        AllotReceiveResponseType response = new AllotReceiveResponseType();
        response.setSuccessful(true);
        try {
            IResult result = useAllotReceiveParamsService.allotReceive(request.getHeadDto(),
                    request.getLineDto());
            if (!result.isSuccessful()) {
                return result.fill(response);
            }

        } catch (LightKitsBizException e) {
            response.setSuccessful(false);
            String mes = StringUtils.substringAfterLast(e.getMessage(), ":");
            response.setMessage(ObjectUtil.isNullOrEmpty(mes) ? e.getMessage() : mes);
        } catch (Exception e) {
            e.printStackTrace();
            response.setSuccessful(false);
            response.setMessage("allot Receive Faild");
        }
        return response;
    }
}
